package com.shenganyuan.smac.change;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.widget.ImageView;
import android.widget.TextView;

import com.oklib.utils.helper.RxUtil;
import com.oklib.utils.network.http.ViseHttp;
import com.oklib.utils.network.http.callback.ACallback;
import com.oklib.utils.network.http.subscriber.ApiCallbackSubscriber;
import com.oklib.utils.view.SnackbarUtil;
import com.oklib.widget.recyclerview.CoreRecyclerView;
import com.oklib.widget.recyclerview.entity.MultiItemEntity;
import com.shenganyuan.smac.App;
import com.shenganyuan.smac.R;
import com.shenganyuan.smac.change.adapter.AdapterChangeInfo;
import com.shenganyuan.smac.common.BaseActivity;
import com.shenganyuan.smac.common.beans.ApiChangeInfo;
import com.shenganyuan.smac.common.beans.ChangeInfoSimple;
import com.shenganyuan.smac.common.beans.ItemBeanTitleBar;
import com.shenganyuan.smac.common.beans.RiskInfo;
import com.shenganyuan.smac.common.beans.RiskResource;
import com.shenganyuan.smac.common.data.Api;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 整改记录详细信息
 *
 * @author Damon
 */

public class ActChangeInfo extends BaseActivity {

    @BindView(R.id.toolbar_back)
    ImageView toolbarBack;
    @BindView(R.id.common_toolbar_center_title)
    TextView commonToolbarCenterTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.recycler_view)
    CoreRecyclerView recyclerView;
    AdapterChangeInfo adapterChangeInfo;
    List<MultiItemEntity> list = new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.act_change_info;
    }

    @Override
    public void initUI(Bundle savedInstanceState) {
    }


    @Override
    public void initData() {
        setTitle("隐患整改信息");
        String pcyhxxid = getIntent().getStringExtra("pcyhxxid");
        if (pcyhxxid == null) {
            return;
        }
        list.clear();
        adapterChangeInfo = new AdapterChangeInfo(list);
        recyclerView.init(adapterChangeInfo);
        getDataFromServer(pcyhxxid);
    }


    /**
     * 获取服务器数据
     */
    private void getDataFromServer(String pcyhxxid) {
        ViseHttp.RETROFIT().create(Api.class).getRiskChangeInfo(pcyhxxid, App.getAccessToken())
                .compose(RxUtil.rxSchedulerHelper()).subscribe(new ApiCallbackSubscriber<>(new ACallback<ApiChangeInfo>() {
            @Override
            public void onSuccess(ApiChangeInfo data) {
                try {
                    if ("true".equalsIgnoreCase(data.getResult())) {
                        list.clear();
                        list.addAll(opearteData(data));
                        adapterChangeInfo.notifyDataSetChanged();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFail(int errCode, String errMsg) {
                SnackbarUtil.showShort(getWindow().getDecorView(), "获取信息失败");
            }
        }));
    }

    private List<MultiItemEntity> opearteData(ApiChangeInfo data) {
        List<MultiItemEntity> list = new ArrayList<>();
        String title0 = "危险源信息";
        ItemBeanTitleBar titleBar0 = new ItemBeanTitleBar();
        titleBar0.title = title0;
        list.add(titleBar0);
        RiskResource riskResource = data.getGkwxbs();
        if (riskResource != null) {
            list.add(riskResource);
        }
        String title1 = "隐患记录";
        ItemBeanTitleBar titleBar1 = new ItemBeanTitleBar();
        titleBar1.title = title1;
        list.add(titleBar1);
        RiskInfo riskInfo = data.getPcyhxx();
        if (riskInfo != null) {
            list.add(riskInfo);
        }

        String title2 = "整改记录";
        ItemBeanTitleBar titleBar2 = new ItemBeanTitleBar();
        titleBar2.title = title2;
        list.add(titleBar2);
        List<ChangeInfoSimple> changeInfoSimples = data.getPczgxx();
        if (changeInfoSimples != null && changeInfoSimples.size() > 0) {
            for (int i = 0; i < changeInfoSimples.size(); i++) {
                list.add(changeInfoSimples.get(i));
            }
        }
        return list;
    }
}
